﻿using Microsoft.AspNetCore.SignalR;

namespace SignalR.Server.Hubs.HubFilters
{
    public class NotifyClientFilter:IHubFilter
    {
        private readonly ILogger<StopClientFilter> _logger;
        private readonly IHubContext<NotifyHub> _hubContext;

        public NotifyClientFilter(ILogger<StopClientFilter> logger, IHubContext<NotifyHub> hubContext)
        {
            _logger = logger;
            _hubContext = hubContext;
        }

        public async ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next)
        {
            _logger.LogInformation($"NotifyFilter calling hub method  {invocationContext.HubMethodName}");

            await _hubContext.Clients.All.SendAsync("ExecuteCmd", $"DoSth===={DateTime.Now.ToString()}"); 
            var ret = await next(invocationContext);
              
            return ret;
        }
    }
}
